package it.uai.domainsMigrator;
import java.io.IOException;
import java.net.SocketException;
import java.util.ArrayList;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;

public class FtpMakeDirectories {

	private static final Logger logger = Logger.getLogger(FtpTransporter.class);

	public void makeDir(String host, String username, String pw,
			String remotePath, String sourcePath,
			ArrayList<SingleFile> listaFile) {

		FTPClient ftp = new FTPClient();

		// try to connect
		try {
			ftp.connect(host);

			// login to server
			if (!ftp.login(username, pw)) {
				ftp.logout();
				logger.debug("Nome utente e pw errati");
			}
			int reply = ftp.getReplyCode();

			// FTPReply stores a set of constants for FTP reply codes.
			if (!FTPReply.isPositiveCompletion(reply)) {
				ftp.disconnect();
			}
			ftp.enterLocalPassiveMode();
			logger.debug("Remote system is " + ftp.getSystemType());

			for (SingleFile file : listaFile) {

				String relativePath = remotePath
						+ file.getFile().getAbsolutePath()
								.replace(sourcePath, "");
				// logger.trace("Creo directory: " + relativePath);
				ftp.makeDirectory(relativePath);
				ftp.doCommand("chmod 755", relativePath);

			}

			ftp.logout();
			ftp.disconnect();

		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
